package com.heima.wemedia.service.impl;

import com.heima.apis.article.IArticleClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.service.WmNewsStatisticsService;
import com.heima.wemedia.service.WmUserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import thread.WmThreadLocalUtil;

@Service
public class WmNewsStatisticsServiceImpl implements WmNewsStatisticsService {
    @Qualifier("com.heima.apis.article.IArticleClient")
    @Autowired
    private IArticleClient client;

    @Autowired
    private WmUserService wmUserService;

    /**
     * 图文统计
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult newsDimension(WmNewsPageReqDto dto) {
        Integer userId = WmThreadLocalUtil.getWmUserThreadLocal().getId();
        WmUser wmUser = wmUserService.getById(userId);
        if (wmUser == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        dto.setAuthorId(wmUser.getApAuthorId());
        return client.newsDimension(dto);
    }

    /**
     * 发布详情统计
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult newsPage(WmNewsPageReqDto dto) {
        Integer userId = WmThreadLocalUtil.getWmUserThreadLocal().getId();
        WmUser wmUser = wmUserService.getById(userId);
        if (wmUser == null) return PageResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        dto.setAuthorId(wmUser.getApAuthorId());
        return client.newsPage(dto);
    }

    /**
     * 单篇文章分析
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult newsDetail(WmNewsPageReqDto dto) {
        Integer userId = WmThreadLocalUtil.getWmUserThreadLocal().getId();
        WmUser wmUser = wmUserService.getById(userId);
        if (wmUser == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        dto.setAuthorId(wmUser.getApAuthorId());
        return client.newsDetail(dto);
    }
}
